//-----------------------------------------------------------------------
// <copyright file="MailLetterDAOBase.cs" company="FetCo">
//     Copyright (c) FetCo. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------

using System;
using System.Data;
using System.Data.Common;
using System.Collections;
using FetCo.Framework;
using FetCo.Framework.Data;
using Microsoft.Practices.EnterpriseLibrary.Data;
using ADESSA.Entities;

namespace ADESSA.Data
{    
    public abstract class MailLetterDAOBase
    {
        #region Common methods
        public virtual MailLetter CreateMailLetterFromReader(IDataReader reader)
        {
            MailLetter item = new MailLetter();
            try
            {
                if (!reader.IsDBNull(reader.GetOrdinal("MailLetterId"))) item.MailLetterId = (long)reader["MailLetterId"];
                if (!reader.IsDBNull(reader.GetOrdinal("FullName"))) item.FullName = (string)reader["FullName"];
                if (!reader.IsDBNull(reader.GetOrdinal("Email"))) item.Email = (string)reader["Email"];
                if (!reader.IsDBNull(reader.GetOrdinal("Sex"))) item.Sex = (string)reader["Sex"];
                if (!reader.IsDBNull(reader.GetOrdinal("CreatedDate"))) item.CreatedDate = (DateTime)reader["CreatedDate"];
                if (!reader.IsDBNull(reader.GetOrdinal("CatalogId"))) item.CatalogId = (long)reader["CatalogId"];
                if (!reader.IsDBNull(reader.GetOrdinal("Language"))) item.Language = (string)reader["Language"];
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessCreateMailLetterFromReaderException, ex);
            }
            return item;
        }
        #endregion
        
        #region CreateMailLetter methods
            
        public virtual void CreateMailLetter(MailLetter mailLetter)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("spMailLetterInsert");
                
                database.AddInParameter(dbCommand, "@FullName", DbType.String, mailLetter.FullName);
                database.AddInParameter(dbCommand, "@Email", DbType.String, mailLetter.Email);
                database.AddInParameter(dbCommand, "@Sex", DbType.String, mailLetter.Sex);
                database.AddInParameter(dbCommand, "@CreatedDate", DbType.DateTime, mailLetter.CreatedDate);
                database.AddInParameter(dbCommand, "@CatalogId", DbType.Int64, mailLetter.CatalogId);
                database.AddInParameter(dbCommand, "@Language", DbType.String, mailLetter.Language);
                database.AddOutParameter(dbCommand, "@MailLetterId", DbType.Int64, 8);
                
                DataAccessHelper.ExecuteNonQuery(database, dbCommand);
                mailLetter.MailLetterId = (long)database.GetParameterValue(dbCommand, "@MailLetterId");
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessCreateMailLetterException, ex);
            }
        }

        #endregion

        #region UpdateMailLetter methods
        
        public virtual void UpdateMailLetter(MailLetter mailLetter)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("spMailLetterUpdate");            
                
                database.AddInParameter(dbCommand, "@MailLetterId", DbType.Int64, mailLetter.MailLetterId);
                database.AddInParameter(dbCommand, "@FullName", DbType.String, mailLetter.FullName);
                database.AddInParameter(dbCommand, "@Email", DbType.String, mailLetter.Email);
                database.AddInParameter(dbCommand, "@Sex", DbType.String, mailLetter.Sex);
                database.AddInParameter(dbCommand, "@CreatedDate", DbType.DateTime, mailLetter.CreatedDate);
                database.AddInParameter(dbCommand, "@CatalogId", DbType.Int64, mailLetter.CatalogId);
                database.AddInParameter(dbCommand, "@Language", DbType.String, mailLetter.Language);
                
                DataAccessHelper.ExecuteNonQuery(database, dbCommand);
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessUpdateMailLetterException, ex);
            }
        }
        
        #endregion

        #region DeleteMailLetter methods
        public virtual void DeleteMailLetter(long mailLetterId)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("spMailLetterDelete");
                
                database.AddInParameter(dbCommand, "@MailLetterId", DbType.Int64, mailLetterId);
                
                DataAccessHelper.ExecuteNonQuery(database, dbCommand);
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessDeleteMailLetterException, ex);
            }
        }

        #endregion

        #region GetMailLetter methods
        
        public virtual MailLetter GetMailLetter(long mailLetterId)
        {            
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("spMailLetterGet");
                
                database.AddInParameter(dbCommand, "@MailLetterId", DbType.Int64, mailLetterId);
                
                MailLetter mailLetter =  null;
                using (IDataReader reader = DataAccessHelper.ExecuteReader(database, dbCommand))
                {
                    if (reader.Read())
                    {
                        mailLetter = CreateMailLetterFromReader(reader);
                        reader.Close();
                    }
                }
                return mailLetter;
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessGetMailLetterException, ex);
            }
        }
        
        #endregion

        #region GetMailLetterList methods
        public virtual MailLetterCollection GetMailLetterList(MailLetterColumns orderBy, OrderDirection orderDirection, int page, int pageSize, out int totalRecords)
        {            
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("spMailLetterGetList");
                
                database.AddInParameter(dbCommand, "@OrderBy", DbType.AnsiString, orderBy.ToString());
                database.AddInParameter(dbCommand, "@OrderDirection", DbType.AnsiString, orderDirection.ToString());
                database.AddInParameter(dbCommand, "@Page", DbType.Int32, page);
                database.AddInParameter(dbCommand, "@PageSize", DbType.Int32, pageSize);
                database.AddOutParameter(dbCommand, "@TotalRecords", DbType.Int32, 4);
                
                MailLetterCollection mailLetterCollection = new MailLetterCollection();
                using (IDataReader reader = DataAccessHelper.ExecuteReader(database, dbCommand))
                {
                    while (reader.Read())
                    {
                        MailLetter mailLetter = CreateMailLetterFromReader(reader);
                        mailLetterCollection.Add(mailLetter);
                    }
                    reader.Close();
                }
                totalRecords = (int)database.GetParameterValue(dbCommand, "@TotalRecords");
                return mailLetterCollection;
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessGetMailLetterListException, ex);
            }
        }
        
        public virtual MailLetterCollection GetMailLetterList(MailLetterColumns orderBy, OrderDirection orderDirection)
        {            
            int totalRecords = 0;
            return GetMailLetterList(orderBy, orderDirection, 0, 0, out totalRecords);
        }
        
        #endregion
		
		  #region GetMailLetterList methods
        public virtual MailLetterCollection GetMailLetterListByParam(object orderBy, OrderDirection orderDirection, int page, int pageSize,string Criteria, out int totalRecords)
        {            
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("spMailLetterGetListByParam");
                
                database.AddInParameter(dbCommand, "@OrderBy", DbType.AnsiString, orderBy.ToString());
                database.AddInParameter(dbCommand, "@OrderDirection", DbType.AnsiString, orderDirection.ToString());
                database.AddInParameter(dbCommand, "@Page", DbType.Int32, page);
                database.AddInParameter(dbCommand, "@PageSize", DbType.Int32, pageSize);
				database.AddInParameter(dbCommand, "@Criteria",  DbType.String, Criteria);
                database.AddOutParameter(dbCommand, "@TotalRecords", DbType.Int32, 4);
                
                MailLetterCollection mailLetterCollection = new MailLetterCollection();
                using (IDataReader reader = DataAccessHelper.ExecuteReader(database, dbCommand))
                {
                    while (reader.Read())
                    {
                        MailLetter mailLetter = CreateMailLetterFromReader(reader);
                        mailLetterCollection.Add(mailLetter);
                    }
                    reader.Close();
                }
                totalRecords = (int)database.GetParameterValue(dbCommand, "@TotalRecords");
                return mailLetterCollection;
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessGetMailLetterListException, ex);
            }
        }
        
        public virtual MailLetterCollection GetMailLetterListByParam(object orderBy, OrderDirection orderDirection,string Criteria)
        {            
            int totalRecords = 0;
            return GetMailLetterListByParam(orderBy, orderDirection, 0, 0,Criteria, out totalRecords);
        }
        
        #endregion
    }
}